<?php
    header("Content-Type: text/html; charset=iso-8859-1");
    include "/../includes/session.php";
    include "/../includes/validaUsuario.php";
    require_once "/../includes/DAOs/AdminDAO.php";
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="../css/style.css" rel="stylesheet" type="text/css" />
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
		<script id="jqueryui" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" defer async></script>
		<script src="//www.google.com/jsapi?key=AIzaSyCZfHRnq7tigC-COeQRmoa9Cxr0vbrK6xw"></script>
		<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
		<script>
			var map;
			var geocoder; // = new google.maps.Geocoder();
			var tempMarker = new google.maps.Marker();
			var locationIcon = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png",
				new google.maps.Size(12,20),
				new google.maps.Size(6,20)
			);
			var newShadow = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_shadow.png",
				new google.maps.Size(22,20),
				new google.maps.Point(13,13)
			);
			function initialize() {
				geocoder = new google.maps.Geocoder();
				var mapOptions = {
					zoom: 16,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
				// Try HTML5 geolocation
				if(navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(function(position) {
						var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
						map.setCenter(pos);
					}, function() {
						handleNoGeolocation(true);
					});
				} else {
					// Browser doesn't support Geolocation
					handleNoGeolocation(false);
				}
				
				tempMarker.setOptions({
					icon: locationIcon,
					shadow: newShadow,
					draggable: true
				});
				
				google.maps.event.addListener(map, "click", showTempMarker);
				
				function showTempMarker(e) {
					tempMarker.setPosition(e.latLng);
					document.getElementById('lat').value = e.latLng.lat();
					document.getElementById('lon').value = e.latLng.lng();
					google.maps.event.addListener(tempMarker, "drag", function() {
						document.getElementById('lat').value = tempMarker.getPosition().lat();
						document.getElementById('lon').value = tempMarker.getPosition().lng();
					});
					tempMarker.setMap(map);
				}
			}
			
			function handleNoGeolocation(errorFlag) {
				if (errorFlag) {
					var content = 'Error: The Geolocation service failed.';
				} else {
					var content = 'Error: Your browser doesn\'t support geolocation.';
				}
				var options = {
					map: map,
					position: new google.maps.LatLng(-33.034355,-71.596313),
					content: content
				};
				var infowindow = new google.maps.InfoWindow(options);
				map.setCenter(options.position);
			}
			
			function showAddress(address) {
				geocoder.geocode({'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						var pos = results[0].geometry.location;
						map.setCenter(pos);
						tinyMarker = new google.maps.Marker({
							position: pos,
							map: map,
							icon: locationIcon,
							shadow: newShadow,
							draggable: true
						});
						document.getElementById('lat').value = pos.lat();
						document.getElementById('lon').value = pos.lng();
					} else {
						alert("Direccion no encontrada. Por favor ingresela manualmente.");
					}
				});
			}
			
			function buscar(){
				var aux = document.getElementById('address');
				var address = aux.value + ", Chile";
				var coor = document.getElementById('coor');
				showAddress(address);
				coor.value = 1;
			}
			
			google.maps.event.addDomListener(window, 'load', initialize);
		</script>
		<script type="text/javascript">
			function cambiarEstado(){
				var coor = document.getElementById('coor');
				coor.value = 1;
			}
		</script>
        <title>Go Now - Administrador</title>
    </head>
    <body>
		<div class="image" style="width:968px;">		
			<div class="user">
				<?php
                $adminDAO = new AdminDAO();
                $result = $adminDAO->getNombreUsuario($_SESSION['id_user']);
                list($nombre) = $adminDAO->fetch($result);
                ?>
                <p>Admin: <?php echo $nombre ?> <a href="../includes/logout.php">salir</a></p>
            </div>
			<a href="http://localhost/gonow/indexUser.php">
			<img  src="../css/logo.png"  alt="logo"  style="margin-left:35px;"/> </a> 
		</div>
        <div id="contenido2" class="content" style="height:700px">
			<div id="sidebar">
				<ul style="height:300px">
					<li style="background:#659C08;"><a href="index.php">Eventos</a></li> 
					<li><a href="usuarios.php">Usuarios</a></li>       
					<li><a href="scripts.php">Scripts</a></li>
					<li><a href="agregarEvento.php">Agregar Eventos</a></li>
			  </ul>			
        </div>
			<?php
				if(isset($_GET['id'])){
					$id = $_GET['id'];
					$adminDAO = new AdminDAO();
					$result = $adminDAO->getDatosEvento($id);
					list($id_ev,$nombre,$fecha_inicio,$fecha_termino,$direccion,$precios,$cat,$lat,$lon) = $adminDAO->fetch($result);
					$result2 = $adminDAO->getDescripcion($id);
					if($adminDAO->numRows($result2) > 0){
						list($descripcion) = $adminDAO->fetch($result2);							
					}
					else{
						$descripcion = null;
					}
					?>
					<div style="float:right; width:750px;">
						<div style="float:right; width:420px;">
						 <div style="float: right; margin-right:20px; margin-top:40px; margin-bottom:20px;">
								<table>	
									<tr>
										<td><input type="text" id="address" name="address" value="<?php echo $direccion; ?>" /></td>
										<td><input style="width: 100px; margin-top: -1px;" class="green_btn" onClick="buscar();" type="submit" id="Buscar" name="Buscar" value="Buscar"/></td>
									</tr>
								</table>
						</div>	
							<div id="map_canvas" style="  margin-right:20px ; height: 450px; width: 400px;"></div>
						</div>
			
					<div class="agregar-box">
						<form action="../includes/controllers/adminController.php?action=editarEvento" method="post">
						 <p>Nombre del Evento</p>
                        <input type="text" name="nombre" id="nombre" value="<?php echo $nombre ?>" />
						<p>Fecha Inicio (YYYY-MM-dd hh:mm:ss)</p>
                       <input type="text" name="fecha_inicio" id="fecha_inicio" value="<?php echo $fecha_inicio ?>" />
						<p>Fecha Termino (YYYY-MM-dd hh:mm:ss)</p>
                        <input type="text" name="fecha_termino" id="fecha_termino" value="<?php echo $fecha_termino ?>" />
						<p>Direccion del Evento</p>
                        <input type="text" name="direccion" id="direccion" value="<?php echo $direccion ?>" />
						<p>Precios</p>
                        <input type="text" name="precios" id="precios" value="<?php echo $precios ?>"/>
						<p>Categoria</p>
                        <select id="cat" name="cat" >
							<?php
								$adminDAO = new AdminDAO();
								$result = $adminDAO->getCategorias();
								while(list($id,$nombre) = $adminDAO->fetch($result)){
									?>
									<option value="<?php echo $id; ?>" <?php if($id == $cat) echo "SELECTED"; ?>><?php echo $nombre; ?></option>
									<?php
								}
							?>
						</select>						
						<p>Latitud </p>
                        <input onchange="cambiarEstado();" type="text" id="lat" name="lat" size="15" value="<?php echo $lat ?>" />
						<p>Longitud: </p>
						<input onchange="cambiarEstado();" type="text" id="lon" name="lon" size="15" value="<?php echo $lon ?>" />
						<p>Descripci&oacute;n</p>
						<input type="text" name="descripcion" id="descripcion" value="<?php if($descripcion != null) echo $descripcion; ?>"/>
						<input id="id_ev" name="id_ev" type="hidden" value="<?php echo $id_ev; ?>" />	
						<input id="coor" name="coor" type="hidden" value="0" />							
					</div>			
					<input class="green_btn" type="submit" id="editar" name="editar" value="Editar Evento" style="margin-top:20px; margin-left:120px"/>		
				</form>							
			</div>
					<?php 
					if(isset($_GET['flag'])){
						if($_GET['flag'] == '1'){
							echo "<script type='text/javascript'>alert('Datos guardados exitosamente.')</script";
						}
						if($_GET['flag'] == '0'){
							echo "<script type='text/javascript'>alert('Ha ocurrido un error. Intentelo nuevamente.')</script";
						}
					}
				}
				else{
					?>
					<p>No se ha ingresado el identificador del evento</p>
					<?php
				}
			?>
        </div>
    </body>
	<footer>		
		<p> Proyecto Seminario II Telem&aacute;tica 2012 &copy Gonz&aacute;lez - Orozco - Pino - Villarreal</p>
    </footer>
</html>

